package com.example.joblinker.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.joblinker.common.Result;
import com.example.joblinker.dto.InterviewDTO;
import com.example.joblinker.service.InterviewService;
import com.example.joblinker.util.JwtUtil;
import com.example.joblinker.vo.InterviewVO;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

@RestController
@RequestMapping("/interviews")
public class InterviewController {

    @Resource
    private InterviewService interviewService;

    @Resource
    private JwtUtil jwtUtil;

    /**
     * 安排面试
     */
    @PostMapping
    public Result<InterviewVO> arrangeInterview(@RequestBody @Valid InterviewDTO interviewDTO) {
        InterviewVO vo = interviewService.arrangeInterview(interviewDTO);
        return Result.success(vo);
    }

    /**
     * 获取面试详情
     */
    @GetMapping("/{id}")
    public Result<InterviewVO> getInterviewDetail(@PathVariable Long id) {
        InterviewVO vo = interviewService.getInterviewDetail(id);
        return Result.success(vo);
    }

    /**
     * 获取当前用户的面试列表
     */
    @GetMapping("/user")
    public Result<Page<InterviewVO>> getUserInterviews(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size,
            HttpServletRequest request) {
        Long userId = jwtUtil.getUserIdFromRequest(request);
        Page<InterviewVO> voPage = interviewService.getUserInterviews(userId, page, size);
        return Result.success(voPage);
    }

    /**
     * 获取指定公司的面试列表
     */
    @GetMapping("/company/{companyId}")
    public Result<Page<InterviewVO>> getCompanyInterviews(
            @PathVariable Long companyId,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size) {
        Page<InterviewVO> voPage = interviewService.getCompanyInterviews(companyId, page, size);
        return Result.success(voPage);
    }

    /**
     * 更新面试状态
     */
    @PutMapping("/{id}/status")
    public Result<Void> updateInterviewStatus(
            @PathVariable Long id,
            @RequestParam Integer status) {
        interviewService.updateInterviewStatus(id, status);
        return Result.success();
    }

    /**
     * 提交面试反馈
     */
    @PutMapping("/{id}/feedback")
    public Result<Void> submitFeedback(
            @PathVariable Long id,
            @RequestParam String feedback,
            @RequestParam Integer rating) {
        interviewService.submitFeedback(id, feedback, rating);
        return Result.success();
    }

    /**
     * 取消面试
     */
    @PutMapping("/{id}/cancel")
    public Result<Void> cancelInterview(@PathVariable Long id) {
        interviewService.cancelInterview(id);
        return Result.success();
    }
} 